// pages/person/component/goods.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    productList: {
      type: Array,
      value: [],
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // productList:[]
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 显示删除按钮
     */
    showDeleteButton: function(e) {

      let dpr = app.globalData.dpr
      let winWidth = app.globalData.winWidth
      let cur = 150 / 750 * winWidth
      let temp = Number(cur * dpr)
      let productIndex = e.currentTarget.dataset.productindex
      // console.log('显示删除按钮', e)
      this.setXmove(productIndex, -temp)
    },

    /**
     * 隐藏删除按钮
     */
    hideDeleteButton: function(e) {
      let productIndex = e.currentTarget.dataset.productindex

      // console.log('隐藏删除按钮', productIndex)
      this.setXmove(productIndex, 0)
    },

    /**
     * 设置movable-view位移
     */
    setXmove: function(productIndex, xmove) {
      let productList = this.data.productList
      productList[productIndex].xmove = xmove
      this.setData({
        productList: productList
      })
    },

    /**
     * 处理movable-view移动事件
     */
    handleMovableChange: function(e) {
      if (e.detail.source === 'friction') {
        if (e.detail.x < -30) {
          this.showDeleteButton(e)
        } else {
          this.hideDeleteButton(e)
        }
      } else if (e.detail.source === 'out-of-bounds' && e.detail.x === 0) {
        this.hideDeleteButton(e)
      }
    },

    /**
     * 处理touchstart事件
     */
    handleTouchStart(e) {
      let productIndex = e.currentTarget.dataset.productindex
      //判断,如果点击的不是当前已经弹出删除按钮的商品,那么关闭删除按钮
      let productList = this.data.productList
      productList.forEach((item, index) => {
        if (index != productIndex) {
          this.setXmove(index, 0)
        } else {

        }
      })

      this.startX = e.touches[0].pageX
    },

    /**
     * 处理touchend事件
     */
    handleTouchEnd(e) {

      if (e.changedTouches[0].pageX < this.startX && e.changedTouches[0].pageX - this.startX <= -30) {
        this.showDeleteButton(e)
      } else if (e.changedTouches[0].pageX > this.startX && e.changedTouches[0].pageX - this.startX < 30) {
        this.showDeleteButton(e)
      } else {
        this.hideDeleteButton(e)
      }
    },
    fangdou(callback, delay) {
      console.log(callback)
      var timer;
      return function(e) {
        //返回给addEventListener
        // 2. 如果之前有定时器,则表示之前也有执行过,先将定时器清除
        if (timer) {
          // console.log('清理', timer)
          clearTimeout(timer);
        }
        //1. 创建一个定时器,延时执行代码
        timer = setTimeout(() => {
          //执行回调
          console.log(111)
          callback(e);
          timer = null; //执行完毕后手动清理
        }, delay);
        // console.log('设置', timer)
      };
    },

    // 进入详情
    godetail(e) {
      console.log(e)
      let item = e.currentTarget.dataset.item;
      this.triggerEvent('godetail', item)

    },
    ontapdel(e) {
      console.log(e)
      let item = e.currentTarget.dataset.item;
      this.triggerEvent('ontapdel', item)
    }
  }
})